﻿using System;
using System.Drawing;
using Company.BLL.DuLieuChuan;
using Company.BLL.GC;
using Company.BLL.KDT;
using Company.Interface.GC;
using Company.Interface.SXXK;
using Janus.Windows.GridEX;
using Janus.Windows.GridEX.EditControls;
using Company.BLL;
using System.Windows.Forms;
namespace Company.Interface
{
    public partial class ChungTuForm : BaseForm
    {
        public ChungTuCollection collection;
        public ChungTu ctDetail;
        private int stt=0;
        public ChungTuForm()
        {
            InitializeComponent();
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            if (ctDetail == null)
            {
                ctDetail = new ChungTu();               
            }
           
            ctDetail.TenChungTu = txtChungTu.Text.Trim();
            ctDetail.SoBanChinh = Convert.ToInt16(txtSoBanChinh.Text);
            ctDetail.SoBanSao = Convert.ToInt16(txtSoBanPhu.Text);
            ctDetail.FileUpLoad = txtFileUpload.Text;            
            if (ctDetail.SoBanChinh == 0 && ctDetail.SoBanSao == 0)
            {
                ShowMessage("Số bản chính hoặc số bản sao phải lớn hơn không.", false);
                txtSoBanChinh.Focus();
                return;
            }
            if (this.OpenType == OpenFormType.Insert)
            {
                ctDetail.STTHang = ++stt;
            }
            if (collection == null)
                collection = new ChungTuCollection();
            foreach (ChungTu ct in collection)
            {
                if (ct.TenChungTu.ToUpper() == ctDetail.TenChungTu.ToUpper())
                {
                    ShowMessage("Đã có chứng từ này rồi", false);
                    ctDetail = null;
                    return;
                }
            }
            collection.Add(ctDetail);          
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (this.OpenType == OpenFormType.Edit)
            {
                collection.Add(ctDetail);
            }
            this.Close();
        }

        private void ChungTuForm_Load(object sender, EventArgs e)
        {
            if (this.OpenType==OpenFormType.Edit)
            {
                txtChungTu.Text = ctDetail.TenChungTu;
                txtFileUpload.Text = ctDetail.FileUpLoad;
                txtSoBanChinh.Text = ctDetail.SoBanChinh.ToString();
                txtSoBanPhu.Text = ctDetail.SoBanSao.ToString();
                btnAddNew.Text = "Sửa";
            }
            else
                if (this.OpenType == OpenFormType.View)
                {
                    btnAddNew.Visible = false;
                }
            foreach (ChungTu ct in collection)
            {
                if (stt<ct.STTHang)
                {
                    stt = ct.STTHang;
                }
            }
        }

        private void txtFileUpload_ButtonClick(object sender, EventArgs e)
        {
            DialogResult kq = openFileDialog1.ShowDialog();
            if (kq == DialogResult.OK)
            {
                txtFileUpload.Text = openFileDialog1.FileName;
            }
        }

     
    }
}
